package com.webwork.lims.orders.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webwork.lims.orders.dao.OrderDao;
import com.webwork.lims.orders.dataobject.Order;
import com.webwork.lims.orders.service.OrderService;
import com.webwork.lims.util.Paging;

@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderDao orderDao;
	
	@Override
	public int count(Order queryObj) {
		return orderDao.count(queryObj);
	}

	@Override
	public List<Order> query(Order queryObj, Paging paging) {
		return orderDao.query(queryObj, paging.getCurrPage(), paging.getPageSize());
	}
	
	@Override
	public List<Order> query(Order queryObj) {
		return this.query(queryObj, new Paging(0, 0));
	}
	
	@Override
	public Date queryFirstDate() {
		return orderDao.queryFirstDate();
	}

	@Override
	public Order get(String orderId) {
		Order queryObj = new Order();
		queryObj.setOrderId(orderId);
		List<Order> order = this.query(queryObj);
		if (order != null && order.size() > 0) {
			if (order.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return order.get(0);
			}
		} else {
			System.out.println("找不到该运单!");
		}
		return null;
	}

	@Override
	public void insert(Order order) {
		orderDao.insert(order);
	}
	
	@Override
	public void update(Order order) {
		orderDao.update(order);
	}
	
	@Override
	public void save(Order order) {
		if (this.get(order.getOrderId()) != null) {
			this.update(order);
		} else {
			this.insert(order);
		}
	}

	@Override
	public List<String> queryAddressByGroup(int zoneCode) {
		return orderDao.queryAddressByGroup(zoneCode);
	}
	
	public List<Order> getByTransId(String transId) {
		Order queryObj = new Order();
		queryObj.setTransId(transId);
		return this.query(queryObj);
	}
	
}
